package com.gk.carshopsys.service;

import com.gk.carshopsys.dao.*;
import com.gk.carshopsys.pojo.Car;
import com.gk.carshopsys.pojo.Part;
import com.gk.carshopsys.pojo.PartOrder;
import com.gk.carshopsys.pojo.PartOrderImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PartOrderServiceImpl implements PartOrderService{

    @Autowired
    PartOrderDao partOrderDao;
    @Autowired
    PartDao partDao;
    @Autowired
    CustDao custDao;
    @Autowired
    EmpDao empDao;

    @Override
    public int addPartOrderByList(List<PartOrderImport> list) {
        return partOrderDao.addPartOrderByList(list);
    }

    @Override
    public List<PartOrder> findPartOrder(HashMap<String,String> params) {
        String part = params.get("part");
        String emp = params.get("emp");
        String ordernum = params.get("ordernum");
        String cust = params.get("cust");
        String state = params.get("state");
        String start_time = params.get("start_time");
        String end_time = params.get("end_time");
        return partOrderDao.findPartOrder(params);
    }


/**
 *  新增订单，库存处理
 * @return
 * -1：没有这个配件
 * -2：没有这个客户
 * -3：没有这个员工
 * -4：库存不足
 *
 */

    @Override
    public int addPartOrder(Long partid, Long custid, String empnum, Integer quantity, String state, String remarks) {
        PartOrder partorder=new PartOrder();

        if(partDao.findPartId(partid).isEmpty()){
            return -1;

        }
        if (custDao.findCustId(custid).isEmpty()){
            return -2;
        }
        if (empDao.findEmpByEmpno(empnum).isEmpty()){
            return -3;
        }else{
            Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
            partorder.setEmpid(empid);
        }

        //获取库存
        Long stock = partDao.findQuantityById(partid);
        if(stock<quantity){
            return -4;
        }else{
            Part part = new Part();
            part.setId(partid);
            part.setQuantity(stock-quantity);
            partDao.updateQuantityById(part);
        }

        partorder.setPartid(partid);
        partorder.setCustid(custid);
        partorder.setQuantity(quantity);
        partorder.setState(state);
        partorder.setRemarks(remarks);

        String now = new Date(System.currentTimeMillis()).toString();

        partorder.setCreate_time(now);
        partorder.setUpdate_time(now);

        Date date = date = new Date(System.currentTimeMillis());
//获取年月日
        java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
        sformat.applyPattern("yyyy");
        String year = sformat.format(date);
        sformat.applyPattern("MM");
        String mouth = sformat.format(date);
        sformat.applyPattern("dd");
        String day = sformat.format(date);

        Integer empid=empDao.findEmpByEmpno(empnum).get(0).getId();
//        生成3位随机数
        int randow=(int)(Math.random()*(900+100));
        String ordernum =  year+mouth+day+partid+empid+custid+randow;

        partorder.setOrdernum(ordernum);


        int result = partOrderDao.addPartOrder(partorder);
        return result;
    }

    @Override
    public int updatePartOrderById(PartOrder partOrder,Long quantity) {
        if(partOrder.getState().equals("已取消")){
            //获取库存
            Long stock = partDao.findQuantityById(partOrder.getPartid());
            Part part = new Part();

            part.setId(partOrder.getPartid());
            part.setQuantity(stock+quantity);
            partDao.updateQuantityById(part);

        }
        String now = new Date(System.currentTimeMillis()).toString();
        partOrder.setUpdate_time(now);

        System.out.println(partOrder);
        return partOrderDao.updatePartOrderById(partOrder);
    }

    @Override
    public int delPartOrder(List<Integer> partorder) {
        return partOrderDao.delPartOrder(partorder);
    }


}
